<template>
  <div class="cc-main-menu">
    <q-expansion-item :header-inset-level="calcLevel" v-for="menu in menuList.filter(v => !v.hidden)" :key="menu.id" :icon="menu.meta.icon"
      :label="menu.meta.title" :header-style="headerStyleActive(menu)" :value="openMenu(menu)" :expand-icon-class="menu.children ? '' : 'hide-icon'"
      :group="menu.id" @click="go(menu)">
      <template v-slot:header>
        <q-item-section avatar>
          <q-avatar :icon="menu.meta.icon" />
        </q-item-section>

        <q-item-section>
          {{ menu.meta.title }}
        </q-item-section>

        <q-item-section side>
          <menu-badge  v-if="menu.num" :menu="menu" />
        </q-item-section>
      </template>
      <base-menu-item v-if="menu.children" :menuList="menu.children" :level="level + 1" />
    </q-expansion-item>
  </div>
</template>

<script>
import { openURL, copyToClipboard, debounce } from 'quasar';
import menuBadge from './menuBadge';

export default {
  name: 'base-menu-item',
  components: { menuBadge },
  inject: ['reload'],
  props: {
    menuList: Array,
    level: Number,
  },
  data() {
    return {
      num: null
    };
  },
  watch: {},
  computed: {
    calcLevel() {
      if (this.level === 1) {
        return 0;
      }
      return (this.level - 1) * 0.1;
    },
  },
  methods: {
    go(u) {
      if (u.children) {
        return;
      }
      if (u.meta && u.meta.internalOrExternal) {
        openURL(`/#${u.path}`);
      } else if (u.fullPath !== this.$route.fullPath) {
        // 路径不同就切换下
        this.$router.replace(u);
      } else if (this.$route.query && u.query) {
        // 参数不同也切换下
        if (this.$route.query.id !== u.query.id) {
          this.$router.push(u);
        } else {
          this.reload();
        }
      } else if (u.fullPath === this.$route.fullPath) {
        this.reload();
      }
    },
    shipTypeGo() {
      this.reload();
    },
    match(item, result) {
      if (this.$route.path === item.path) {
        if (this.$route.query && item.query) {
          if (this.$route.query.id === item.query.id) {
            result.color = '#1890ff';
          }
        } else {
          result.color = '#1890ff';
        }
      }
      if (item.children && item.children.length > 0) {
        item.children.forEach((child) => { this.match(child, result); });
      }
    },
    headerStyleActive(item) {
      // 这个算法要修改一下，当前节点的颜色是由后代节点是否能和当前访问匹配上来决定的
      const result = this.level === 1 ? { padding: '0 10px 0 2px' } : {};
      this.match(item, result);
      return result;
    },
    openMenu(item) {
      const result = {};
      this.match(item, result);
      return result.color === '#1890ff';
    },
    itemStyleActive(item) {
      if (item.path === this.$route.path) {
        return {
          color: '#1890ff',
          backgroundColor: '#e6f7ff',
          borderRight: '0.2em solid #1890ff',
        };
      }
      return {};
    },
  },
  created() {
  },
  mounted() { },
};
</script>
<style lang="stylus">
.cc-main-menu
  line-height: 1.5;
  font-size: 14px;
  .q-item__section--avatar
    min-width 30px
  .q-item
    padding 0 0 0 10px
</style>
